package com.example.zy5_project_jetpack.mvp.view.SofaFragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zy5_project_jetpack.Manager.RetrofitManager;
import com.example.zy5_project_jetpack.R;
import com.example.zy5_project_jetpack.mvp.view.SofaFragment.Adapter.ImageAdapter;
import com.example.zy5_project_jetpack.mvp.view.SofaFragment.Adapter.VideoAdapter;
import com.example.zy5_project_jetpack.mvp.view.SofaFragment.Bean.ImageBean;
import com.example.zy5_project_jetpack.mvp.view.SofaFragment.Bean.VideoBean;
import com.google.gson.Gson;

import java.util.List;


public class VideoFragment extends Fragment {


    private VideoAdapter videoAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_video, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String videoHttp  = RetrofitManager.getInstance().getVideoHttp();
                    if (videoHttp != null) {
                        final VideoBean videoBean = new Gson().fromJson(videoHttp, VideoBean.class);

                        final List<VideoBean.DataBeanX.DataBean> data = videoBean.getData().getData();
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                videoAdapter.addList(data);
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }


    private void initView(View view) {
        RecyclerView recycler_video = view.findViewById(R.id.recycler_video);
        recycler_video.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycler_video.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        videoAdapter = new VideoAdapter(getActivity());
        recycler_video.setAdapter(videoAdapter);
    }
}